package com.yun.algorithmproblem.leetcode;


public class Leetcode135 {
    public int candy(int[] ratings) {
        int n = ratings.length;
        int ret = 1;
        int inc = 1, dec = 0, pre = 1;
        for (int i = 1; i < n; i++) {
            if (ratings[i] >= ratings[i - 1]) {
                //处于递增序列总
                dec = 0;//递减序列长度在递增序列中为0
                pre = ratings[i] == ratings[i - 1] ? 1 : pre + 1;
                ret += pre;
                inc = pre;
            }else {
                //处于递减序列中
                dec++;
                if(dec==inc){
                    //当递减序列长度和递增序列长度相等时,把递增序列的最后一个同学分配到递减序列中
                    dec++;
                }
                ret+=dec;
                pre=1;
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] ratings = {1, 0, 2};
        Leetcode135 obj = new Leetcode135();
        System.out.println(obj.candy(ratings));
    }
}
